package com.cwj.lab2;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import com.cwj.lab1.myArrayList;

public class lab2_Test {
    public static void main(String[] args) {
        BT_test test=new BT_test();
        test.test_init();
    }
}
class BT_test{
    static public myArrayList<String> readTree(String path){
        myArrayList<String>list=new myArrayList<>();
        try(BufferedReader in=new BufferedReader(new FileReader(path))){
            String str=null;
            while((str=in.readLine())!=null){
                list.addLast(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }
    public void test_init(){
        String path="src/src/com/cwj/lab2/BT_init_test_file";
        myArrayList<String>list=readTree(path);
        for(int i=0;i<list.size();i+=1){
            BT<Character> tree=new BT();
            BT<Character> cTree=new BT();
            tree.initTree(list.get(i));
            tree.show();
            System.out.print("\t是否一样:"+(list.get(i).compareTo(tree.toString())==0));
            System.out.print("\t是否回文："+tree.isSymmetricTree());
            System.out.print("\t结点总数："+tree.nodeSum());
            System.out.print("\t叶子节点总数:"+tree.lfNodeSum());
            System.out.println("\t高度："+tree.getHigh());
            //前序遍历
            String str1=null,str2=null;
            System.out.print("前序遍历：\n递归->");
            System.out.print(str1=tree.preOrder());
            System.out.print("\t迭代->");
            System.out.print(str2=tree.preOrder2());
            System.out.print("\t是否一致："+(str1.compareTo(str2)==0));
            System.out.println();
            //中序遍历
            System.out.print("中序遍历：\n递归->");
            System.out.print(str1=tree.midOrder());
            System.out.print("\t迭代->");
            System.out.print(str2=tree.midOrder2());
            System.out.print("\t是否一致："+(str1.compareTo(str2)==0));
            System.out.println();
            //后序遍历
            System.out.print("后序遍历：\n递归->");
            System.out.print(str1=tree.postOrder());
            System.out.print("\t迭代->");
            System.out.print(str2=tree.postOrder2());
            System.out.print("\t是否一致："+(str1.compareTo(str2)==0));
            System.out.println();

            //构造前中树
            String tStr=tree.toString();
            System.out.print("由中序和前序构造树：");
            cTree.initPreMid(tree.preOrder(),tree.midOrder());
            cTree.show();
            System.out.println("与原树是否一致:"+(cTree.toString().compareTo(tStr)==0));

            //构造后中树
            System.out.print("由后序和前序构造树：");
            cTree.initPostMid(tree.postOrder(),tree.midOrder());
            cTree.show();
            System.out.println("与原树是否一致:"+(cTree.toString().compareTo(tStr)==0));

            //最小公共祖先
            char one='D',two='M';
            BT.Node nn=null;
            System.out.print(one+"和"+two+"的最小公共祖先：");
            if((nn=cTree.lca(one,two))==null){
                System.out.println("两个结点没有同时存在");
            }else{
                System.out.println(nn.data);
            }

            //树的左视图
            System.out.println("树的左视图："+cTree.lefTree());

            System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");

        }
    }



}


